package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Test5 {
    public static void main(String[] args) throws Exception{
        String packName = Test5.class.getPackage().getName();
        File dir = new File(
                Test5.class.getResource(".").toURI()
        );
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : subs){
            String fileName = file.getName();
            System.out.println(fileName);
            String className = fileName.substring(0,fileName.indexOf("."));
            Class cls =Class.forName(packName + "."+ className);
            Object obj = cls.newInstance();
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods){
                if (
                        method.getParameterCount() == 0
                        &&
                             method.getModifiers() == Modifier.PUBLIC
                        &&
                                method.getName().contains("s")
                ){
                    System.out.println("执行方法" + method.getName() +"()");
                    method.invoke(obj);
                }
            }
        }
    }
}
